<?php
namespace app\admin\controller;

class Banner extends Common
{
    public function index()
    {
        $list= db("banner")->paginate();
        return $this->fetch('index',compact("list"));
    }
    public function edit()
    {
        if(request()->isPost()){
            $data=input();
            $insert['title']=$data['title'];
            $insert['status']=$data['status'];
            $insert['url']=$data['url'];
            $insert['image']=$data['image'];
            $insert['sort']=$data['sort'];
            $insert['create_time']=time();
            if(input("id")){
                $res= db("banner")->where(['id'=>$data['id']])->update($insert);
            }else{
                $res=db("banner")->insert($insert);
            }
            if($res!==false){
                return $this->renderSuccess('操作成功',url('Banner/index'));
            }
            return $this->renderError('操作失败');
        }
        $where['id']=input("id");
        $info=db("banner")
            ->where($where)
            ->find();
        return $this->fetch('edit',compact("info"));
    }
    public function banner_del()
    {
        if(request()->isPost()){
            $id=input("id");
             $res= db("banner")->where(['id'=>$id])->delete();
            if($res!==false){
                return   $this->renderSuccess('删除成功',url('banner/index'));
            }
            return  $this->renderError('删除失败',url('banner/index'));
        }
        return  $this->renderError('非法访问',url('banner/index'));
    }
}
